<template>
  <div id="app">
    <el-header class="header">
      <el-menu :default-active="activeIndex" class="top-nav-menu" mode="horizontal" @select="handleSelect"
               text-color="#090909" active-text-color="#ff0000">
        <el-menu-item index="0">
          <img src="../img/login.png" height="65" width="157"/>
        </el-menu-item>
        <el-menu-item index="1" @click="switchView('/')">发现音乐</el-menu-item>
        <el-menu-item index="2" @click="switchView('/likes')">我喜欢</el-menu-item>
        <el-menu-item index="3" @click="switchView('/follow')">关注</el-menu-item>
        <el-menu-item index="4" @click="switchView('/creator-platform')">创作者平台</el-menu-item>
        <el-menu-item index="5" @click="switchView('/discover-music')">客户端下载</el-menu-item>
        <el-menu-item index="6">
          <input type="text" placeholder="搜索歌曲、歌手、MV"/>
        </el-menu-item>

        <el-menu-item index="7" class="login-menu-item">
          <el-dropdown v-if="userInfo.userName" @command="handleCommand">
            <span class="el-dropdown-link">
              <el-avatar :size="40" :src="userInfo.userAvatar" shape="circle"></el-avatar>
              <span style="margin-left: 8px; color: red">{{ userInfo.userName }}</span>
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="orderOut">我的订单</el-dropdown-item>
              <el-dropdown-item command="personal">个人中心</el-dropdown-item>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <template v-else>
            <el-button @click="goToLogin">请先登录</el-button>
          </template>
        </el-menu-item>
      </el-menu>
    </el-header>
  </div>
</template>

<script>
import { getUserInfo, removeUserInfo, removeToken } from "@/utils/auth";

export default {
  name: 'App',
  data() {
    return {
      activeIndex: '1',
      userInfo: {}
    };
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    goToLogin() {
      // 跳转到登录页面的方法
      this.$router.push('/login');
    },
    handleCommand(command) {
      if (command === 'personal') {
        // 跳转到个人中心的方法
        this.$router.push('/user');
      } else if (command === 'orderOut') {
        this.$router.push('/order');
      } else if (command === 'logout') {
        // 退出登录的方法
        alert('退出登录');
        removeToken(); // 清除Token
        removeUserInfo(); // 清空用户信息
        this.userInfo = {}; // 清空组件中的用户信息
        this.$router.push('/login'); // 跳转到登录页面
      }
    },
    switchView(path) {
      this.$router.push(path);
    }
  },
  created() {
    this.userInfo = getUserInfo(); // 获取用户信息
  }
};
</script>

<style>
/* 基本样式 */
body, html {
  margin: 0;
  padding: 0;
  font-family: '微软雅黑', Arial, sans-serif;
}

.header {
  width: 100%;
  background-color: #ffffff;
  border-bottom: 1px solid #e5e5e5;
  position: fixed; /* 固定导航栏 */
  top: 0;
  z-index: 1000; /* 确保导航栏在最上层 */
}

.top-nav-menu {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.top-nav-menu .el-menu-item {
  color: #090909;
  text-decoration: none;
  padding: 5px 35px;
  font-size: 16px;
}

.top-nav-menu .el-menu-item:hover {
  background-color: #f2f2f2;
}

.search-bar {
  margin-left: 20px; /* 调整搜索栏和左边的距离 */
  margin-right: 20px; /* 调整搜索栏和右边的距离 */
}

.search-bar input {
  padding: 5px 10px;
  border: 1px solid #e5e5e5;
  border-radius: 5px;
  font-size: 14px;
}

.login-menu-item {
  display: flex;
  justify-content: center;
  align-items: center;
}

.el-dropdown-link {
  cursor: pointer;
  color: #090909;
}

.el-dropdown-menu__item {
  text-align: center;
}

.content {
  padding: 20px;
  padding-top: 60px; /* 根据导航栏的高度调整，确保内容不被覆盖 */
}

</style>